//
//  VehicleController.m
//  Collection
//
//  Created by Neelam Shehzadi on 10/1/11.
//  Copyright 2011 Confiz Solution. All rights reserved.
//


#define SCROLLVIEW_CONTENT_HEIGHT 460
#define SCROLLVIEW_CONTENT_WIDTH  320 


#import "VehicleController.h"
#import "SignatureVC.h"
#import "CarSectionTableViewController.h"
#import "CustomerSignatureController.h"


CGFloat animatedDistance;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 280;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
//anyView has been defined for different kind of views to avoid keyboard when entering data in textfield.
UIView *anyView;

@implementation VehicleController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [vehicleManufacturerDropDownView release];
    [vehicleManufacturerArr_ release];
    
    [fuelLevelArr_ release];
    [fuelLevelDropDownView release];
    
    [drivableArr_ release];
    [drivableDropDownView release];
    
    [carSuppliedArr_ release];
    [carSuppliedDropDownView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Booking Detai..2";
    
    UIBarButtonItem *settingNavigationItem = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonPreviewAction:)];
    settingNavigationItem.style = UIBarButtonItemStyleBordered;
    [self.navigationItem setRightBarButtonItem:settingNavigationItem];
    [settingNavigationItem release];
    
    cache = [NSUserDefaults standardUserDefaults ];
    
    objScrollView_.contentSize = CGSizeMake(320, 900);
    
    
    vehicleManufacturerArr_ = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"FORD",@"SUZUKI",@"TOYOTA",nil]]; 
    [vMBtn_ setTitle:[vehicleManufacturerArr_ objectAtIndex:0] forState:UIControlStateNormal];
	 //vMBtn_.titleLabel.textAlignment = UITextAlignmentLeft;
    
	[vMBtn_ setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
	vehicleManufacturerDropDownView = [[DropDownView alloc] initWithArrayData:vehicleManufacturerArr_ cellHeight:25 heightTableView:75 paddingTop:0 paddingLeft:0 paddingRight:0 refView:vMBtn_ animation:BLENDIN openAnimationDuration:.5 closeAnimationDuration:.5];
	
	vehicleManufacturerDropDownView.delegate = self;
	
	[objScrollView_ addSubview:vehicleManufacturerDropDownView.view];
    

    drivableArr_ = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"YES",@"NO",nil]];
	[drivableBtn_ setTitle:[drivableArr_ objectAtIndex:0] forState:UIControlStateNormal];
	[drivableBtn_ setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

	drivableDropDownView = [[DropDownView alloc] initWithArrayData:drivableArr_ cellHeight:25 heightTableView:60 paddingTop:0 paddingLeft:0 paddingRight:0 refView:drivableBtn_ animation:BLENDIN openAnimationDuration:.5 closeAnimationDuration:.5];
	
	drivableDropDownView.delegate = self;
	
	[objScrollView_ addSubview:drivableDropDownView.view];
    

    carSuppliedArr_ = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"YES",@"NO",nil]]; 
    [carSuppliedBtn_ setTitle:[carSuppliedArr_ objectAtIndex:0] forState:UIControlStateNormal];
[carSuppliedBtn_ setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
	carSuppliedDropDownView = [[DropDownView alloc] initWithArrayData:carSuppliedArr_ cellHeight:25 heightTableView:60 paddingTop:0 paddingLeft:0 paddingRight:0 refView:carSuppliedBtn_ animation:BLENDIN openAnimationDuration:.5 closeAnimationDuration:.5];
	
	carSuppliedDropDownView.delegate = self;
	
	[objScrollView_ addSubview:carSuppliedDropDownView.view];
    

    fuelLevelArr_ = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"Empty",@"1/4",@"1/2",@"3/4",@"Full",nil]]; 
    [fuelBtn_ setTitle:[fuelLevelArr_ objectAtIndex:0] forState:UIControlStateNormal];
    [fuelBtn_ setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    
    fuelLevelDropDownView = [[DropDownView alloc] initWithArrayData:fuelLevelArr_ cellHeight:25 heightTableView:100 paddingTop:0 paddingLeft:0 paddingRight:0 refView:fuelBtn_ animation:BLENDIN openAnimationDuration:1 closeAnimationDuration:1];
	
	fuelLevelDropDownView.delegate = self;
	
	[objScrollView_ addSubview:fuelLevelDropDownView.view];
	
	if ([cache valueForKey:@"reg"]!=[NSNull null]) { 
		
		regTF_.text = [cache valueForKey:@"reg"];
	}
    
	if ([cache valueForKey:@"Model"]!=[NSNull null]) { 
		
		modelTF_.text = [cache valueForKey:@"Model"];
	}
	
	if ([cache valueForKey:@"color"]!=[NSNull null]) { 
		
		colorTF_.text = [cache valueForKey:@"color"];
	}
	
	

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)buttonPreviewAction:(id)sender
{
    [cache setValue:vMBtn_.titleLabel.text forKey:@"vehicleManufacturer"];
    [cache setValue:mileageTF_.text forKey:@"vehicleMileage"];
    [cache setValue:taxDescTF_.text forKey:@"taxDesc"];
    [cache setValue:drivableBtn_.titleLabel.text forKey:@"drivable"];
    [cache setValue:personalItemTF_.text forKey:@"personalItems"];
    [cache setValue:recoveryTF_.text forKey:@"recoveryPaid"];
    [cache setValue:wheelTF_.text forKey:@"wheelTrims"];
    [cache setValue:toolsTF_.text forKey:@"tools"];
    [cache setValue:keysTF_.text forKey:@"keys"];
    [cache setValue:alarmTF_.text forKey:@"alarm"];
    [cache setValue:radioCodeTF_.text forKey:@"radioCode"];
    [cache setValue:carSuppliedBtn_.titleLabel.text forKey:@"carSupplied"];
    [cache setValue:fuelBtn_.titleLabel.text forKey:@"fuelLevel"];
    
    
    CustomerSignatureController *objSignatureVC = [[CustomerSignatureController alloc] init];
    [self.navigationController pushViewController:objSignatureVC animated:YES];
    [objSignatureVC release];
}




- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    /*if([textView.text length] <= 0)
     {
     textView.text = @"About Me";
     }*/
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
#pragma mark -
#pragma mark DropDownViewDelegate

-(void)dropDownCellSelected:(NSInteger)returnIndex{
    if(selectedBtnTag == 0)
    {
        [vMBtn_ setTitle:[vehicleManufacturerArr_ objectAtIndex:returnIndex] forState:UIControlStateNormal];
    }
    else if(selectedBtnTag == 1)
    {
        [drivableBtn_ setTitle:[drivableArr_ objectAtIndex:returnIndex] forState:UIControlStateNormal];

    }
    else if(selectedBtnTag == 2)
    {
        [carSuppliedBtn_ setTitle:[carSuppliedArr_ objectAtIndex:returnIndex] forState:UIControlStateNormal];

    }
    else
    {
       [fuelBtn_ setTitle:[fuelLevelArr_ objectAtIndex:returnIndex] forState:UIControlStateNormal];
    }
}

-(IBAction)actionButtonClick:(UIButton*)sender{
    selectedBtnTag = sender.tag;
    if(sender.tag == 0)
    {
        [vehicleManufacturerDropDownView openAnimation];
    }
    else if(sender.tag == 1)
    {
        [drivableDropDownView openAnimation];
    }
    else if(sender.tag == 2)
    {
        [carSuppliedDropDownView openAnimation];
    }
    else
    {
        [fuelLevelDropDownView openAnimation];
    }
    
}

#pragma mark -
#pragma mark TextField Delegate

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
	activeField = textField;
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
	hideph.hidden = YES;
	textField.keyboardAppearance = UIKeyboardAppearanceAlert;

	if(textField!=mileageTF_||textField!=taxDescTF_)
	{
	anyView = objScrollView_;
	}
	
 	CGRect textFieldRect = [anyView.window convertRect:textField.bounds fromView:textField];
 	CGRect viewRect =
 	[anyView.window convertRect:self.view.bounds fromView:objScrollView_];
 	CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
 	CGFloat numerator =
 	midline - viewRect.origin.y
 	- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
 	CGFloat denominator =
 	(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
 	* viewRect.size.height;
 	CGFloat heightFraction = numerator / denominator;
 	if (heightFraction < 0.0)
 	{ 
		heightFraction = 0.0;
 	}
 	else if (heightFraction > 1.0)
 	{
		heightFraction = 1.0;
 	}
 	UIInterfaceOrientation orientation =
 	[[UIApplication sharedApplication] statusBarOrientation];
 	if (orientation == UIInterfaceOrientationPortrait ||
		orientation == UIInterfaceOrientationPortraitUpsideDown)
 	{
		animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
 	}
 	else
 	{
		animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
 	}
	
 	CGRect viewFrame = objScrollView_.frame;
 	viewFrame.origin.y -= animatedDistance;
	
 	[UIView beginAnimations:nil context:NULL];
 	[UIView setAnimationBeginsFromCurrentState:YES];
 	[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
 	[anyView setFrame:viewFrame];
 	[UIView commitAnimations];
	
	//mileage textField
	if (textField.tag == 123) {
		hideph.hidden = NO;
		hideph.frame = CGRectMake(0.00, 160.00, 322.0, 44.0);

		
	}
	
	
}

-(IBAction)hidekeypad
{
	[mileageTF_ resignFirstResponder];
	hideph.hidden = YES;


}



- (void)textFieldDidEndEditing:(UITextField *)textField
{
	CGRect viewFrame = objScrollView_.frame;
	viewFrame.origin.y += animatedDistance;
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationBeginsFromCurrentState:YES];
	[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
	[anyView setFrame:viewFrame];
	[UIView commitAnimations];
}



@end
